package com.wwm.findit;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.daimajia.swipe.SimpleSwipeListener;
import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.adapters.BaseSwipeAdapter;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by maaibin on 2015/5/20.
 */

public class VerticalListViewAdapter extends BaseSwipeAdapter
{
	private Context mContext;
	private ArrayList<Item> itemArray;
	private View view;
	VerticalListViewAdapter self;

	private RequestQueue mRequestQueue;

	public VerticalListViewAdapter(Context mContext)
	{
		this.mContext = mContext;
		this.self = this;
	}

	public void setData(ArrayList<Item> itemArray)
	{
		this.itemArray = itemArray;
	}

	@Override
	public int getSwipeLayoutResourceId(int position)
	{
		return R.id.vertical_listview_item;
	}

	@Override
	public View generateView(int position, ViewGroup parent)
	{
		View v = LayoutInflater.from(mContext).inflate(R.layout.publish_item, null);

		SwipeLayout swipeLayout = (SwipeLayout) v.findViewById(getSwipeLayoutResourceId(position));
		swipeLayout.addSwipeListener(new SimpleSwipeListener()
		{
			@Override
			public void onOpen(SwipeLayout layout)
			{
				//YoYo.with(Techniques.Tada).duration(500).delay(100).playOn(layout.findViewById(R.id.button_publish));
				layout.setClickToClose(true);
			}
		});
		swipeLayout.setOnDoubleClickListener(new SwipeLayout.DoubleClickListener()
		{
			@Override
			public void onDoubleClick(SwipeLayout layout, boolean surface)
			{

			}
		});
		view = v;
		v.findViewById(R.id.button_publish).setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View view)
			{
				String md5 = (String)view.getTag();
				String username = GlobalData.username;
				String lat = Double.toString(GlobalData.currentLat);
				String lng = Double.toString(GlobalData.currentLng);

				Intent intent = new Intent("android.intent.action.BlankActivity");
				mContext.startActivity(intent);

				mRequestQueue = Volley.newRequestQueue(mContext);
				Map<String, String> map = new HashMap<String, String>();
				map.put("md5", md5);
				map.put("username", username);
				map.put("Lat", lat);
				map.put("Lng", lng);

				Log.i("FindIt", map.toString());

				/*
				*  $Lng = (float)$obj->{'Lng'};
		$Lat = (float)$obj->{'Lat'};
		$md5 = $obj->{'md5'};
		$username = $obj->{'username'};
				* */

				JsonObjectRequest jr = new JsonObjectRequest(Request.Method.POST, GlobalData.PUBLISH_URL, new JSONObject(map), new Response.Listener<JSONObject>()
				{
					@Override
					public void onResponse(JSONObject response)
					{
						Log.i("FindIt", response.toString());
						try
						{
							int retCode = response.getInt("success");
							if (retCode == 0)
							{
								Log.i("FindIt", response.getString("message"));
								Toast.makeText(mContext, response.getString("message"), Toast.LENGTH_SHORT);
							}
							else
							{
								Log.i("FindIt", response.getString("message"));
								Toast.makeText(mContext, response.getString("message"), Toast.LENGTH_SHORT);
								return ;
							}
						} catch (JSONException e)
						{
							e.printStackTrace();
						}

					}
				}, new Response.ErrorListener()
				{
					@Override
					public void onErrorResponse(VolleyError error)
					{
						Log.i("FindIt", error.getMessage());
					}
				});
				mRequestQueue.add(jr);

			}
		});

		return v;
	}

	@Override
	public void fillValues(int position, View convertView)
	{
		TextView t1 = (TextView) convertView.findViewById(R.id.text_name);
		TextView t2 = (TextView) convertView.findViewById(R.id.text_level);
		TextView t3 = (TextView) convertView.findViewById(R.id.text_value);
		Button btn = (Button) convertView.findViewById(R.id.button_publish);

		if (itemArray != null)
		{
			Item item = itemArray.get(position);
			t1.setText("物品名称："+item.itemname);
			t2.setText("物品等级："+Integer.toString(item.level));
			t3.setText("物品价值"+Integer.toString(item.value));
			btn.setTag(item.md5);
		}
	}

	@Override
	public int getCount()
	{
		// return 30;
		if (itemArray != null)
		{
			return itemArray.size();
		}

		return 0;
	}

	@Override
	public Object getItem(int position)
	{
		return null;
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}
}


